﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Common.Utilities;
using Common.Utilities.Test;
using PdfLib;

namespace PdfLib.Test.PdfParser
{
    [ManualTestCase("Image reader", "PDF parser")]
    public partial class TestCase_ImageReader : UserControl
    {
        public TestCase_ImageReader()
        {
            InitializeComponent();
        }

        private void btn_Open_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "PDF files(*.pdf)|*.pdf|All files(*.*)|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txt_PdfPath.Text = dlg.FileName;
                int pageCount = PdfPropertyReader.GetPageCount(this.txt_PdfPath.Text);
                this.num_Page.Value = 1;
                this.num_Page.Maximum = pageCount;

                this.OpenPage(this.txt_PdfPath.Text, (int)this.num_Page.Value);
            }
        }

        private void btn_Open2_Click(object sender, EventArgs e)
        {
            this.OpenPage(this.txt_PdfPath.Text, (int)this.num_Page.Value);
        }

        private void OpenPage(string pdfFilePath, int pageNum)
        {
            try
            {
                Image pageImg = PdfImageReader.GetPageImage(pdfFilePath, pageNum);
                if(pageImg !=null)
                {
                    Bitmap bmp = new Bitmap(pageImg);
                    SizeF pageSize = PdfPropertyReader.GetPageSize(pdfFilePath, pageNum);
                    Bitmap img0=new Bitmap((int)pageSize.Width, (int)pageSize.Height);
                    Graphics g0 = Graphics.FromImage(img0);
                    g0.DrawImage(
                        pageImg, 
                        new Rectangle(0, 0, img0.Width, img0.Height),
                        new Rectangle(0, 0, pageImg.Width, pageImg.Height), 
                        GraphicsUnit.Pixel);
                    this.pic_Img.Image = img0;
                    g0.Dispose();

                    ImageProcesser imgProc = new ImageProcesser(bmp, pageSize);

                    /*
                    List<VerticalLine> vLines = imgProc.ReadVerticalLines();
                    Bitmap img1=new Bitmap((int)pageSize.Width, (int)pageSize.Height);
                    Graphics g1 = Graphics.FromImage(img1);
                    if(vLines !=null && vLines.Count>0)
                    {
                        foreach(VerticalLine vLine in vLines)
                        {
                            g1.DrawLine(new Pen(Color.Black, 2), vLine.X, vLine.Y1, vLine.X, vLine.Y2);
                        }
                    }
                    this.pic_VLines.Image = img1;
                    g1.Dispose();

                    List<HorizontalLine> hLines = imgProc.ReadHorizontalLines();
                    Bitmap img2=new Bitmap((int)pageSize.Width, (int)pageSize.Height);
                    Graphics g2 = Graphics.FromImage(img2);
                    if(hLines !=null && hLines.Count>0)
                    {
                        foreach(HorizontalLine hLine in hLines)
                        {
                            g2.DrawLine(new Pen(Color.Black, 2), hLine.X1, hLine.Y, hLine.X2, hLine.Y);
                        }
                    }
                    this.pic_HLines.Image = img2;
                    g2.Dispose();
                    */

                    List<Rectangle> rects = imgProc.ReadRectangles();
                    Bitmap img3=new Bitmap((int)pageSize.Width, (int)pageSize.Height);
                    Graphics g3 = Graphics.FromImage(img3);
                    if(rects !=null && rects.Count>0)
                    {
                        foreach(Rectangle rect in rects)
                        {
                            g3.DrawRectangle(new Pen(Color.Black,2), rect);
                        }
                    }
                    this.pic_Rects.Image = img3;
                    g3.Dispose();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
